package com.iflysse.controller;

import com.iflysse.common.ResultEnum;
import com.iflysse.pojo.bo.CommonBoResp;
import com.iflysse.pojo.bo.req.ClassBoReq;
import com.iflysse.pojo.bo.req.StudentInTeacherClassBoReq;
import com.iflysse.pojo.bo.req.TeachingClassIdBoReq;
import com.iflysse.pojo.bo.resp.StudentBoResp;
import com.iflysse.pojo.bo.resp.StudentListBoResp;
import com.iflysse.pojo.bo.resp.StudentPlainBoResp;
import com.iflysse.pojo.bo.resp.StudentPlainListBoResp;
import com.iflysse.pojo.vo.common.CommonVoResp;
import com.iflysse.pojo.vo.req.ClassVoReq;
import com.iflysse.pojo.vo.req.StudentInTeacherClassVoReq;
import com.iflysse.pojo.vo.req.TeachingClassIdVoReq;
import com.iflysse.pojo.vo.resp.StudentListResp;
import com.iflysse.pojo.vo.resp.StudentListVoResp;
import com.iflysse.services.StudentService;
import com.iflysse.services.TeacherClassService;
import com.iflysse.util.CommonUtil;
import com.iflysse.util.FileUpdateUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
@RequestMapping("/classStudent")
@Api(value = "老师端班级学生接口")
@Log4j
public class ClassStudentController {


    @Autowired
   private StudentService studentService;

    @Autowired
   private TeacherClassService teacherClassService;


    /**
     * 班级添加学生时 根据行政班级查询学生列表
     */
    @PostMapping("/queryAllStudentByAdministrativeClass")
    @ApiOperation(value = "班级添加学生时根据行政班级查询学生列表")
    public StudentListVoResp doQueryAllStudentByAdministrativeClass(ClassVoReq classVoReq){

        StudentListVoResp studentListVoResp=new StudentListVoResp();

        try {
           log.info("班级添加学生时根据行政班级查询学生列表参数bean:"+classVoReq);
            ClassBoReq classBoReq= CommonUtil.objToObj(classVoReq, ClassBoReq.class);
            log.info("老师端根据行政班查询对应得学生列表参数为："+classBoReq);
            StudentPlainListBoResp studentListBoResp= studentService.getStudentPlainListByClass(classBoReq);
            //得到对象中的集合
            List<StudentPlainBoResp> studentBoRespList= studentListBoResp.getStudentPlainBoResps();
            List<StudentListResp>result=new ArrayList<StudentListResp>();
            //类型转换
            studentBoRespList.forEach(studentBoResp -> {
                StudentListResp studentListResp=new StudentListResp();
                studentListResp.setStudentId(studentBoResp.getStudentId());
                studentListResp.setStudentName(studentBoResp.getStudentName());
                studentListResp.setNumber(studentBoResp.getNumber());
                result.add(studentListResp);
            });
            studentListVoResp.setResult(result);
            studentListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("老师端根据行政班查询对应得学生列表响应信息:"+studentListVoResp);

            return studentListVoResp;
        }catch (Exception e){
            log.error("老师端班级添加学生时,根据行政班级查询学生列表出现异常:"+e);
            CommonUtil.printError(studentListVoResp);
            return studentListVoResp;
        }
    }


    /**
     * 班级添加学生时 根据教学班级编号查询学生列表
     */
    @PostMapping("/queryAllStudentByTeachingClassId")
    @ApiOperation(value = "班级添加学生时根据教学班级编号查询学生列表")
    public StudentListVoResp doQueryAllStudentByTeachingClassId(TeachingClassIdVoReq teachingClassIdVoReq) {
        StudentListVoResp studentListVoResp=new StudentListVoResp();
        try{

            log.info("班级添加学生时根据教学班级编号查询学生列表参数bean:"+teachingClassIdVoReq);
            TeachingClassIdBoReq teachingClassIdBoReq= CommonUtil.objToObj(teachingClassIdVoReq, TeachingClassIdBoReq.class);
//            StudentListBoResp studentListBoResp=   studentService.getStudentListByTeachingClassId(teachingClassIdBoReq);
            StudentListBoResp studentListBoResp=   studentService.getStudentListByTeacherClass(teachingClassIdBoReq);
            List<StudentBoResp>studentBoRespList=studentListBoResp.getStudentBoRespList();
            List<StudentListResp>result=new ArrayList<StudentListResp>();
            studentBoRespList.forEach(studentBoResp -> {
                StudentListResp studentListResp=new StudentListResp();
                studentListResp.setStudentName(studentBoResp.getStudentName());//学生姓名
                studentListResp.setStudentId(studentBoResp.getStudentId());//学生编号
                studentListResp.setNumber(studentBoResp.getNumber());//学号
                result.add(studentListResp);
            });
            studentListVoResp.setResult(result);
            studentListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("老师端根据教学班级查询对应得学生列表响应信息:"+studentListVoResp);
            return studentListVoResp;
        }catch (Exception e){
            log.info("根据教学班级编号查询学生列表出现异常，信息为："+e);
            CommonUtil.printError(studentListVoResp);
            return studentListVoResp;
        }
    }


    /**
     * 老师端教学班级添加学生时
     * 批量将学生列表和老师的教学班级绑定
     */
    @PostMapping("/addStudentToTeacherClass")
    @ApiOperation(value = "老师端教学班级添加学生时批量将学生列表和老师的教学班级绑定")
    public CommonVoResp doAddStudentToTeacherClass(StudentInTeacherClassVoReq studentInTeacherClassVoReq) {
        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("老师端教学班级添加学生时批量将学生列表和老师的教学班级绑定参数bean:"+studentInTeacherClassVoReq);
            StudentInTeacherClassBoReq studentInTeacherClassBoReq=CommonUtil.objToObj(studentInTeacherClassVoReq,StudentInTeacherClassBoReq.class);
            studentInTeacherClassBoReq.setStudentIdList(Arrays.asList(studentInTeacherClassVoReq.getStudentIdList().split(",")));
            log.info("教师端教学班级添加学生时批量将学生列表和老师的教学班级绑定调用业务接口参数:"+studentInTeacherClassBoReq);
            CommonBoResp commonBoResp= teacherClassService.addStudentToTeacherClass(studentInTeacherClassBoReq);
            log.info("教师端教学班级添加学生时批量将学生列表和老师的教学班级绑定业务接口响应bean:"+commonBoResp);

            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);

            return commonVoResp;

        }catch (Exception e){
            log.error("教师端教学班级添加学生时批量将学生列表和老师的教学班级绑定出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }


    /**
     * 点击教学班级进来查看的教学班级学生的学生列表
     * 根据教学班级获取学生信息
     */
    @PostMapping("/queryStudentsByTeacherClassId")
    @ApiOperation(value = "点击教学班级进来查看的教学班级学生的学生列表根据教学班级获取学生信息")
    public StudentListVoResp doQueryStudentsByTeacherClassId(TeachingClassIdVoReq teachingClassIdVoReq){
        StudentListVoResp studentListVoResp=new StudentListVoResp();
        try{
           log.info("点击教学班级进来查看的教学班级学生的学生列表根据教学班级获取学生信息参数bean:"+teachingClassIdVoReq);
            TeachingClassIdBoReq teachingClassIdBoReq=CommonUtil.objToObj(teachingClassIdVoReq,TeachingClassIdBoReq.class);
            StudentListBoResp studentListBoResp=  studentService.getStudentListByTeacherClass(teachingClassIdBoReq);
            List<StudentBoResp> studentBoRespList=  studentListBoResp.getStudentBoRespList();
            List<StudentListResp>result=new ArrayList<StudentListResp>();
            studentBoRespList.forEach(studentBoResp -> {
                StudentListResp studentListResp=new StudentListResp();
                studentListResp.setStudentId(studentBoResp.getStudentId());//学生编号
                studentListResp.setStudentName(studentBoResp.getStudentName());//学生姓名
                studentListResp.setHeadImage(FileUpdateUtil.prefixResources+studentBoResp.getHeadImage());//学生头像
                studentListResp.setNumber(studentBoResp.getNumber());//学号
                studentListResp.setClassNo(studentBoResp.getClassNo());//行政班名称
                //最近一次登录时间
                studentListResp.setRecentLoginTime(CommonUtil.dateSourceToString(studentBoResp.getLastLoginDate(),CommonUtil.pattern));
                result.add(studentListResp);
            });
            studentListVoResp.setResult(result);
            studentListVoResp.setCode(ResultEnum.SUCCESS.getCode());
            studentListVoResp.setMsg(ResultEnum.SUCCESS.getMsg());
            log.info("老师端点击教学班级进来查看的教学班级学生的学生列表根据教学班级获取学生列表响应信息:"+studentListVoResp);
            return studentListVoResp;
        }catch (Exception e){
            log.error("点击教学班级进来查看的教学班级学生的学生列表根据教学班级获取学生信息出现异常："+e);
            CommonUtil.printError(studentListVoResp);
            return studentListVoResp;
        }
    }


    /**
     * 老师端教学班级信息中班级学生列表中移除学生/批量移除
     */
    @PostMapping("/removeStudentForTeacherClass")
    @ApiOperation(value = "老师端教学班级信息中班级学生列表中移除学生/批量移除")
    public CommonVoResp doRemoveStudentForTeacherClass(StudentInTeacherClassVoReq studentInTeacherClassVoReq){
        CommonVoResp commonVoResp=new CommonVoResp();
        try{

            log.info("老师端教学班级信息中班级学生列表中移除学生/批量移除参数bean:"+studentInTeacherClassVoReq);

            StudentInTeacherClassBoReq studentInTeacherClassBoReq=CommonUtil.objToObj(studentInTeacherClassVoReq,StudentInTeacherClassBoReq.class);
            studentInTeacherClassBoReq.setStudentIdList(Arrays.asList(studentInTeacherClassVoReq.getStudentIdList().split(",")));
            log.info("老师端教学班级信息中班级学生列表中移除学生/批量移除调用业务接口参数:"+studentInTeacherClassBoReq);
            CommonBoResp commonBoResp=  teacherClassService.removeStudentFromTeacherClass(studentInTeacherClassBoReq);

            CommonUtil.printBoResultToVoResult(commonBoResp,commonVoResp);
            log.info("老师端教学班级信息中班级学生列表中移除学生/批量移除响应信息："+commonVoResp);

            return commonVoResp;
        }catch (Exception e){
            log.error("老师端教学班级信息中班级学生列表中移除学生/批量移除出现异常："+e);
            CommonUtil.printError(commonVoResp);
            return commonVoResp;
        }
    }

}
